package org.teachertools.web.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.teachertools.web.struts.BaseActionForm;

public class EleveForm extends BaseActionForm {
    private static final long serialVersionUID = 304259798370885762L;

    private Long id;
    private java.lang.String prenom;
    private java.lang.String nom;
    private java.lang.String classe;
    private Boolean isUpdate;

    public Long getId() {
        return this.id;
    }

    public void setId(Long pId) {
        this.id = pId;
    }

    public java.lang.String getPrenom() {
        return this.prenom;
    }

    public void setPrenom(java.lang.String pPrenom) {
        this.prenom = pPrenom;
    }

    public java.lang.String getNom() {
        return this.nom;
    }

    public void setNom(java.lang.String pNom) {
        this.nom = pNom;
    }

    public java.lang.String getClasse() {
        return this.classe;
    }

    public void setClasse(java.lang.String pClasse) {
        this.classe = pClasse;
    }

    public Boolean getIsUpdate() {
        return this.isUpdate;
    }

    public void setIsUpdate(Boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    @Override
    public void reset() {
        this.id = null;
        this.prenom = null;
        this.nom = null;
        this.classe = null;
        this.isUpdate = Boolean.FALSE;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        String valAction = request.getParameter(mapping.getParameter());
        if (getMessage(request, "button.search").equals(valAction)) {
            return validate_search(request);
        }
        if (getMessage(request, "button.save").equals(valAction)) {
            return validate_save(request);
        }

        return new ActionErrors();
    }

    public ActionErrors validate_search(HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (StringUtils.isEmpty(this.prenom) == false && this.prenom.length() > 30) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.prenom")));
        }
        if (StringUtils.isEmpty(this.nom) == false && this.nom.length() > 30) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.nom")));
        }
        if (StringUtils.isEmpty(this.classe) == false && this.classe.length() > 10) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.classe")));
        }
        return errors;
    }

    public ActionErrors validate_save(HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (StringUtils.isEmpty(this.prenom)) {
            errors.add("libelle", new ActionMessage("error.required", getMessage(request, "field.prenom")));
        }
        if (StringUtils.isEmpty(this.prenom) == false && this.prenom.length() > 30) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.prenom")));
        }
        if (StringUtils.isEmpty(this.nom)) {
            errors.add("libelle", new ActionMessage("error.required", getMessage(request, "field.nom")));
        }
        if (StringUtils.isEmpty(this.nom) == false && this.nom.length() > 30) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.nom")));
        }
        if (StringUtils.isEmpty(this.classe) == false && this.classe.length() > 10) {
            errors.add("libelle", new ActionMessage("error.fieldtoolong", getMessage(request, "field.classe")));
        }
        return errors;
    }

}
